﻿using MathNet.Numerics;
using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Device
{
    public class DevXZAxleYKD2405PR : DevBase
    {
        public DevXZAxleYKD2405PR(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }

        //先配置原点信号 01 06 00 43 00 02 XX XX   然后配置回原点模式
        //回原点模式   0   正向限位 
        //01 06 00 31 00 00 D8 05
        //回原点模式  1 反向限位
        //01 06 00 31 00 01 19 C5
        //这些配置完成后 遇到光开会停止  如果遇到光开后还继续移动的话需要查询一下补偿是多少 重新设置一下补偿



        /// <summary>
        /// XOrZ轴使能
        /// </summary>
        /// <returns></returns>
        public bool Enable(string Type)
        {
            bool temperature = false;
            try
            {
                //站点
                byte stationid = 0x00;
                if (Type == "X")
                {
                    stationid = 0x01;
                }
                else if (Type == "Z")
                {
                    stationid = 0x02;
                }
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x29;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 06 00 29 00 01 99 C2
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;






            //byte[] bytesSend = new byte[] { 0x01, 0x06, 0x00, 0x29, 0x00, 0x01, 0x99, 0xC2 };
            //byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
            //if (bytesRecv != null && bytesRecv.Length == 8)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}


        }

        /// <summary>
        /// XOrZ轴失能
        /// </summary>
        /// <returns></returns>
        public bool Noenable(string Type)
        {
            bool temperature = false;
            try
            {
                //站点
                byte stationid = 0x00;
                if (Type == "X")
                {
                    stationid = 0x01;
                }
                else if (Type == "Z")
                {
                    stationid = 0x02;
                }
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x29;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 06 00 29 00 00 58 02
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;
        }

        /// <summary>
        /// XOrZ回原点
        /// </summary>
        /// <returns></returns>
        public bool GoOrigin(string Type)
        {
            bool temperature = false;

            try
            {
                //站点
                byte stationid = 0x00;
                if (Type == "X")
                {
                    stationid = 0x01;
                }
                else if (Type == "Z")
                {
                    stationid = 0x02;
                }
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x30;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 06 00 30 00 01 48 05
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    int Number = 0;

                    while (true)
                    {
                        Thread.Sleep(1000);
                        if (GetState(Type) == 0)
                        {
                            temperature = true;
                            break;
                        }
                        if (Number > 20)
                        {
                            temperature = false;
                            break;
                        }
                        Number++;
                    }
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }

            return temperature;
        }


        /// <summary>
        /// XOrZ轴移动
        /// </summary>
        /// <returns></returns>
        public bool Mov(string Type, int Distance)
        {

            //移动7000步
            //发 01 10 00 24 00 02 04 00 00 1B 58 FB 4E
            //收 01 10 00 24 00 02 01 C3
            //启动
            //发  01 06 00 27 00 05 F9 C2
            //收   01 06 00 27 00 05 F9 C2
            bool temperature = false;
            try
            {
                //站点
                byte stationid = 0x00;
                if (Type == "X")
                {
                    stationid = 0x01;
                }
                else if (Type == "Z")
                {
                    stationid = 0x02;
                }
                //功能码
                byte funcode = 0x10;
                //起始地址
                ushort startAddr = 0x24;
                //发送数据
                byte[] bytesData = new byte[7];
                bytesData[0] = (byte)(Distance & 0x00FF);
                bytesData[1] = (byte)((Distance & 0xFF00) >> 8);
                bytesData[2] = 0x00;
                bytesData[3] = 0x00;
                bytesData[4] = 0x04;
                bytesData[5] = 0x02;
                bytesData[6] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] bytesRecv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (bytesRecv != null && bytesRecv.Length == 8)
                {
                    Thread.Sleep(1000);
                    funcode = 0x06;
                    startAddr = 0x27;
                    byte[] bytesDataSend = new byte[2];
                    bytesDataSend[1] = 0x00;
                    bytesDataSend[0] = 0x05;
                    List<byte> bytesSend = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesDataSend);
                    bytesRecv = session_.sendAndReadByteLock(bytesSend.ToArray(), 300);
                    if (bytesRecv != null && bytesRecv.Length == 8)
                    {
                        int Number = 0;

                        while (true)
                        {
                            Thread.Sleep(1000);
                            if (GetState(Type) == 0)
                            {
                                temperature = true;
                                break;
                            }
                            if (Number > 20)
                            {
                                temperature = false;
                                break;
                            }
                            Number++;
                        }
                    }
                    else
                    {
                        temperature = false;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return temperature;

        }

        /// <summary>
        /// X电机状态停止代表运行到位
        /// </summary>
        /// <returns></returns>
        public int GetState(string Type)
        {
            int temperature = 1;
            try
            {
                //站点
                byte stationid = 0x00;
                if (Type == "X")
                {
                    stationid = 0x01;
                }
                else if (Type == "Z")
                {
                    stationid = 0x02;
                }
                //功能码
                byte funcode = 0x03;
                //起始地址
                ushort startAddr = 0x04;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //1运行中 
                //0 停止
                // 01 03 00 04 00 01 C5 CB 
                byte[] bytesRecv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (bytesRecv != null && bytesRecv.Length == 7)
                {
                    if (bytesRecv[4] == 0X00)
                    {
                        temperature = 0;
                    }
                    if (bytesRecv[4] == 0X01)
                    {
                        temperature = 1;
                    }
                }
                else
                {
                    temperature = 1;
                }
            }
            catch (Exception ex)
            {
                temperature = 1;
            }

            return temperature;
        }





























        ///// <summary>
        ///// Z轴使能
        ///// </summary>
        ///// <returns></returns>
        //public bool Zenable()
        //{
        //    //02 06 00 29 00 01 99 F1
        //    byte[] bytesSend = new byte[] { 0x02, 0x06, 0x00, 0x29, 0x00, 0x01, 0x99, 0xF1 };

        //    byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //    if (bytesRecv != null && bytesRecv.Length == 8)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }


        //}

        ///// <summary>
        ///// Z轴失能
        ///// </summary>
        ///// <returns></returns>
        //public bool ZNoenable()
        //{


        //    //02 06 00 29 00 00 58 31
        //    byte[] bytesSend = new byte[] { 0x02, 0x06, 0x00, 0x29, 0x00, 0x00, 0x58, 0x31 };

        //    byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //    if (bytesRecv != null)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// Z回原点
        ///// </summary>
        ///// <returns></returns>
        //public bool Zorigin()
        //{

        //    bool temperature = false;
        //    try
        //    {
        //        //02 06 00 30 00 01 48 36
        //        byte[] bytesSend = new byte[] { 0x02, 0x06, 0x00, 0x30, 0x00, 0x01, 0x48, 0x36 };

        //        byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //        if (bytesRecv != null && bytesRecv.Length == 8)
        //        {
        //            int Number = 0;

        //            while (true)
        //            {
        //                Thread.Sleep(1000);
        //                if (GetZState() == 0)
        //                {
        //                    temperature = true;
        //                    break;
        //                }
        //                if (Number > 20)
        //                {
        //                    temperature = false;
        //                    break;
        //                }
        //                Number++;
        //            }
        //        }
        //        else
        //        {
        //            temperature = false;
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //    return temperature;
        //}


        ///// <summary>
        ///// Z轴移动
        ///// </summary>
        ///// <returns></returns>
        //public bool ZMov()
        //{

        //    //移动17000步
        //    //发 02 10 00 24 00 02 04 00 00 42 68 CE 4E
        //    //收 02 10 00 24 00 02 01 F0
        //    //启动
        //    //发  02 06 00 27 00 05 F9 F1
        //    //收   02 06 00 27 00 05 F9 F1

        //    bool temperature = false;
        //    try
        //    {
        //        byte[] bytesSend = new byte[] { 0x02, 0x10, 0x00, 0x24, 0x00, 0x02, 0x04, 0x00, 0x00, 0x42, 0x68, 0xCE, 0x4E };
        //        byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //        if (bytesRecv != null && bytesRecv.Length == 8)
        //        {
        //            Thread.Sleep(1000);
        //            bytesSend = new byte[] { 0x02, 0x06, 0x00, 0x27, 0x00, 0x05, 0xF9, 0xF1 };
        //            bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //            if (bytesRecv != null && bytesRecv.Length == 8)
        //            {
        //                int Number = 0;

        //                while (true)
        //                {
        //                    Thread.Sleep(1000);
        //                    if (GetZState() == 0)
        //                    {
        //                        temperature = true;
        //                        break;
        //                    }
        //                    if (Number > 20)
        //                    {
        //                        temperature = false;
        //                        break;
        //                    }
        //                    Number++;
        //                }
        //            }
        //            else
        //            {
        //                temperature = false;
        //            }
        //        }


        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //    return temperature;

        //}

        ///// <summary>
        ///// Z电机状态停止代表运行到位
        ///// </summary>
        ///// <returns></returns>
        //public int GetZState()
        //{
        //    int temperature = 1;
        //    try
        //    {
        //        //1运行中 
        //        //0 停止
        //        //02 03 00 04 00 01 C5 F8
        //        byte[] bytesSend = new byte[] { 0x02, 0x03, 0x00, 0x04, 0x00, 0x01, 0xC5, 0xF8 };
        //        byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend, 300);
        //        if (bytesRecv != null && bytesRecv.Length == 7)
        //        {
        //            if (bytesRecv[4] == 0X00)
        //            {
        //                temperature = 0;
        //            }
        //            if (bytesRecv[4] == 0X01)
        //            {
        //                temperature = 1;
        //            }
        //        }
        //        else
        //        {
        //            temperature = 1;
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //    return temperature;
        //}













    }
}
